package android.database;

import java.util.ArrayList;
import java.util.List;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.content.Context;

public class Base {

	int base_index;
	String base_name;
	String main_password;
	Context con;
	List<String> source;
	List<String> name;
	List<String> password;
	List<String> note;
	
	public Base(int index, String base, String pass, Context con)
	{
		base_index = index;
		base_name = base;
		main_password = pass;
		this.con = con;
		source = new ArrayList<String>();
		name = new ArrayList<String>();
		password = new ArrayList<String>();
		note = new ArrayList<String>();
	}
	
	public void openBase()
	{
		try 
		{
			source.clear();
			name.clear();
			password.clear();
			note.clear();
			Database db = new Database(con);
			String enc_data = db.getEncryptedData(base_index);
			byte[] decrypt = enc_data.getBytes();
			String text = changeSize(main_password);
			MessageDigest m = MessageDigest.getInstance("SHA-256");//
			m.update(text.getBytes());
			byte[] temp = m.digest();
			String content = new String(decrypt);
			AES aes = new AES();
			aes.setKey(temp);
			content = aes.Decrypt(content);
			StringBuilder sb = new StringBuilder();
			int j = 1;
			for (int i = 0; i < content.length(); i++)
				if (content.charAt(i) == '\n')
				{
					switch (j)
					{
						case 1: source.add(sb.toString()); break;
						case 2: name.add(sb.toString()); break;
						case 3: password.add(sb.toString()); break;
						case 4: note.add(sb.toString()); break;
					}
					j = 1 + j%4;
					while (sb.length() != 0) sb.deleteCharAt(0);
				}
				else sb.append(content.charAt(i));
		} 
		catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void saveBase()
	{
		try
		{
			String content = "";
			for (int i = 0; i < source.size(); i++) content += source.get(i) + "\n" + name.get(i) + "\n" + password.get(i) + "\n" + note.get(i) + "\n";
			String text = changeSize(main_password);
			MessageDigest m = MessageDigest.getInstance("SHA-256");//
			m.update(text.getBytes());
			byte[] temp = m.digest();
			AES aes = new AES();
			aes.setKey(temp);
			while (content.length()%16 != 0) content += " ";
			content = aes.Encrypt(content);
			Database db = new Database(con);
			db.updateData(base_index, content);
		} 
		 catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	private String changeSize(String password)
	{
		char znak;
		StringBuilder sb = new StringBuilder(password);
		while (sb.length() < 32)
		{
			if (sb.length() == 0) 
				{
				 znak = '0';
				 sb.append(znak);
				}
			else 
				{
				int sum = 0, index = 0;
				for (int j = 0; j < sb.length(); j++) sum += Character.getNumericValue(sb.charAt(j)) * (j + 1);
				for (int j = 0; j < sb.length(); j++) index += Character.getNumericValue(sb.charAt(j)) * (j + 1);
				sum = sum%256;
				index = index%sb.length();
				znak = (char) sum;
				sb.insert(index, znak);
				}
		}
		return sb.toString();
	}
	
	public boolean changeMainPassword(String old_pass, String new_pass, String new_pass2)
	{
		try {
			if (main_password.equals(old_pass) && new_pass.equals(new_pass2))
			{
				MessageDigest m = MessageDigest.getInstance("SHA-512");
				m.update(new_pass.getBytes());
				byte[] temp = m.digest();
				main_password = new_pass;
				saveBase();
				Database db = new Database(con);
				db.changePassword(base_index, new String(temp));
				return true;
			}
			else return false;
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
		}
	}	
	
	public void changeValue(int index, String sourc, String nam, String pass, String not)
	{
		source.set(index, sourc);
		name.set(index, nam);
		password.set(index, pass);
		note.set(index, not);
		saveBase();
	}
	
	public void addValue(String sourc, String nam, String pass, String not)
	{
		source.add(sourc);
		name.add(nam);
		password.add(pass);
		note.add(not);
		saveBase();
	}
	
	public void deleteValue(int index)
	{
		source.remove(index);
		name.remove(index);
		password.remove(index);
		note.remove(index);
		saveBase();
	}
	
	public String getSource(int index) { return source.get(index); }
	public String getName(int index) { return name.get(index); }
	public String getPassword(int index) { return password.get(index); }
	public String getNote(int index) { return note.get(index); }
	public List<String> getSources() { return source; }
}